import $http from '../http'
import $fn from '../fn'
import $store from './storage'
import $toast from './toast'
const Index = {
    getParam( query, ask ){
        const { id } = $store.getUser()
        const ask_str = ask ? '?' : ''
        if($fn.hasObject(query)){
            query.share_id = id
            query = $http.serialize(query, null, 0)
        }
        if( query ) query = ask_str + query
        return query
    },
    async msg(_this, content){
        // const { from, target, webViewUrl } = e
        let { title, img, path, query, api='share', param } = content || {}
        const type = 1
        if(!img){
            const d = await $http.pull(null, api, { param: { type, ...param } })
            if( d == null ){
                $toast.msg.error('分享被禁用')
                return null
            }
            title = d.title
            img = d.image
            path = d.path
            query = d.query ? $http.serialize(query, null, 0) : {}
            if(d.name) query.name = d.name
            if(d.type) query.type = type
            if(d.target) query.target = d.target
            if(d.site) query.site = d.site
        }
        // 将用户 id 传入
        path = (path ? path : '/' + _this.route)
        query.path = path
        query = this.getParam(query, true)
        return {
            title: title ||'分享消息',
            imageUrl: img,
            path: path + query
        }
    },
    async friend(_this,content, type=2){
        // const { from, target, webViewUrl } = e
        let { title, img, query, api='share', param } = content || {}
        if(!img){
            const d = await $http.pull(null, api, { param: { type, ...param } })
            if( d == null ){
                return null
            }
            title = d.title
            img = d.image
            query = d.query ? $http.serialize(query, null, 0) : {}
            if(d.name) query.name = d.name
            if(d.type) query.type = type
            if(d.target) query.target = d.target
            if(d.site) query.site = d.site
        }
        query = this.getParam(query, false)
        return {
            title: title ||'分享朋友圈',
            imageUrl: img,
            query
        }
    },
    async collect(_this,content){
        return await this.friend(_this,content, 3)
    },
    record(e){
        const { query, path } = e
        if( $fn.hasObject(query ) ){
            $http.post(null,'share_record', { param:{ path, ...query } })
        }
    }
}
export default Index